<template>
    <h1>用户借款还款</h1>

    <table>
        <tr>
            <td>借款款项名称</td>
            <td>{{ Show.BM_Name }}</td>
        </tr>

        <tr>
            <td>最高借款金额</td>
            <td>{{ Show.BM_Money }}</td>
        </tr>

        <tr>
            <td>利息</td>
            <td>{{ Show.BMI_Interest }}%</td>
        </tr>

        <tr>
            <td>借款金额</td>
            <td>{{ Show.UBM_Money }}</td>
        </tr>

        <tr>
            <td>利息</td>
            <td>{{ Number( Number(Show.UBM_Money)*Number(Show.BMI_Interest)/100 ).toFixed(2)  }}</td>
        </tr>

        <tr>
            <td>应还款(本+利)</td>
            <td>{{ Number(Show.UBM_Money) +Number( Number(Show.UBM_Money)*Number(Show.BMI_Interest)/100 ) }}</td>
        </tr>

        <tr>
            <td>还款金额</td>
            <td>
                <input type="text" v-model="Money"  >CNY
            </td>
        </tr>

        <tr>
            <td colspan="2">
                <input type="button" value="借款还款" @click="AddUserBorrowMoneyRepay" >
            </td>
        </tr>

    </table>


</template>

<script setup lang="ts">
import { ref,onMounted } from 'vue';
import axios from 'axios';
import { useRoute,useRouter } from 'vue-router';

const Show = ref({
    BM_Id:'',
    BM_Name:'',
    BM_Money:'',
    BMI_Interest:'',
    UBM_Money:'',
    UBM_Id:''
})

const router = useRouter();

const route = useRoute();

console.log(route.params.Id);


const Money = ref('');

onMounted(()=>{
    GetUserBorrowMoneyInfo();
})

const GetUserBorrowMoneyInfo = ()=>{
    axios({
        method:'get',
        url:'http://localhost:56707/api/Borrow/GetUserBorrowMoneyInfo',
        params:{
            UBMId:route.params.Id
        }
    })
    .then(res=>{
        console.log(res.data);
        Show.value = res.data[0];
    })
    .catch(err=>{
        console.log(err);
    })
}

const AddUserBorrowMoneyRepay = ()=>{
    axios({
        method:'post',
        url:'http://localhost:56707/api/Borrow/AddUserBorrowMoneyRepay',
        data:{
            UBM_Id:route.params.Id,
            UBR_Money:Money.value
        }
    })
    .then(res=>{
        console.log(res.data);
        if(res.data>0){
            alert("用户借款还款成功");
            router.push('/ShowUserBorrowPage')
        }
        else{
            alert("用户借款还款失败")
        }
    })
    .catch(err=>{
        console.log(err);
    })
}


</script>
